﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类UserSigningCenterRelation。
    /// </summary>
    public class UserSigningCenterRelation
    {
        public UserSigningCenterRelation()
        { }
        #region  成员方法


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.UserSigningCenterRelation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_UserSigningCenterRelation(");
            strSql.Append("RecordId,CenterId,CenterName,SignerId,SignerName,BelongDepartId,BelongDepartName,BelongCompanyId,BelongCompanyName,BelongCityId,BelongCityName)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@CenterId,@CenterName,@SignerId,@SignerName,@BelongDepartId,@BelongDepartName,@BelongCompanyId,@BelongCompanyName,@BelongCityId,@BelongCityName)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "CenterId", DbType.Guid, model.CenterId);
            db.AddInParameter(dbCommand, "CenterName", DbType.AnsiString, model.CenterName);
            db.AddInParameter(dbCommand, "SignerId", DbType.Guid, model.SignerId);
            db.AddInParameter(dbCommand, "SignerName", DbType.AnsiString, model.SignerName);
            db.AddInParameter(dbCommand, "BelongDepartId", DbType.Guid, model.BelongDepartId);
            db.AddInParameter(dbCommand, "BelongDepartName", DbType.AnsiString, model.BelongDepartName);
            db.AddInParameter(dbCommand, "BelongCompanyId", DbType.Guid, model.BelongCompanyId);
            db.AddInParameter(dbCommand, "BelongCompanyName", DbType.AnsiString, model.BelongCompanyName);
            db.AddInParameter(dbCommand, "BelongCityId", DbType.Guid, model.BelongCityId);
            db.AddInParameter(dbCommand, "BelongCityName", DbType.AnsiString, model.BelongCityName);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.UserSigningCenterRelation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_UserSigningCenterRelation set ");
            strSql.Append("CenterId=@CenterId,");
            strSql.Append("CenterName=@CenterName,");
            strSql.Append("SignerId=@SignerId,");
            strSql.Append("SignerName=@SignerName,");
            strSql.Append("BelongDepartId=@BelongDepartId,");
            strSql.Append("BelongDepartName=@BelongDepartName,");
            strSql.Append("BelongCompanyId=@BelongCompanyId,");
            strSql.Append("BelongCompanyName=@BelongCompanyName,");
            strSql.Append("BelongCityId=@BelongCityId,");
            strSql.Append("BelongCityName=@BelongCityName");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "CenterId", DbType.Guid, model.CenterId);
            db.AddInParameter(dbCommand, "CenterName", DbType.AnsiString, model.CenterName);
            db.AddInParameter(dbCommand, "SignerId", DbType.Guid, model.SignerId);
            db.AddInParameter(dbCommand, "SignerName", DbType.AnsiString, model.SignerName);
            db.AddInParameter(dbCommand, "BelongDepartId", DbType.Guid, model.BelongDepartId);
            db.AddInParameter(dbCommand, "BelongDepartName", DbType.AnsiString, model.BelongDepartName);
            db.AddInParameter(dbCommand, "BelongCompanyId", DbType.Guid, model.BelongCompanyId);
            db.AddInParameter(dbCommand, "BelongCompanyName", DbType.AnsiString, model.BelongCompanyName);
            db.AddInParameter(dbCommand, "BelongCityId", DbType.Guid, model.BelongCityId);
            db.AddInParameter(dbCommand, "BelongCityName", DbType.AnsiString, model.BelongCityName);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_UserSigningCenterRelation ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.UserSigningCenterRelation GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CenterId,CenterName,SignerId,SignerName,BelongDepartId,BelongDepartName,BelongCompanyId,BelongCompanyName,BelongCityId,BelongCityName from T_UserSigningCenterRelation ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.UserSigningCenterRelation model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CenterId,CenterName,SignerId,SignerName,BelongDepartId,BelongDepartName,BelongCompanyId,BelongCompanyName,BelongCityId,BelongCityName ");
            strSql.Append(" FROM T_UserSigningCenterRelation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.UserSigningCenterRelation> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CenterId,CenterName,SignerId,SignerName,BelongDepartId,BelongDepartName,BelongCompanyId,BelongCompanyName,BelongCityId,BelongCityName ");
            strSql.Append(" FROM T_UserSigningCenterRelation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.UserSigningCenterRelation> list = new List<Chenxin.Lib.Business.Entity.UserSigningCenterRelation>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.UserSigningCenterRelation ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.UserSigningCenterRelation model = new Chenxin.Lib.Business.Entity.UserSigningCenterRelation();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CenterId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CenterId = new Guid(ojb.ToString());
            }
            model.CenterName = dataReader["CenterName"].ToString();
            ojb = dataReader["SignerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SignerId = new Guid(ojb.ToString());
            }
            model.SignerName = dataReader["SignerName"].ToString();
            ojb = dataReader["BelongDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BelongDepartId = new Guid(ojb.ToString());
            }
            model.BelongDepartName = dataReader["BelongDepartName"].ToString();
            ojb = dataReader["BelongCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BelongCompanyId = new Guid(ojb.ToString());
            }
            model.BelongCompanyName = dataReader["BelongCompanyName"].ToString();
            ojb = dataReader["BelongCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BelongCityId = new Guid(ojb.ToString());
            }
            model.BelongCityName = dataReader["BelongCityName"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

